{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(graph-concepts)=\n",
    "# Graph state machine\n",
    "\n",
    "Learn about the serving graph state machines."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**In this section**\n",
    "- [The event object](#the-event-object)\n",
    "- [The context object](#the-context-object)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The event object\n",
    "\n",
    "The graph state machine accepts an event object (similar to a Nuclio event) and passes \n",
    "it along the pipeline. An event object hosts the event `body` along with other attributes \n",
    "such as `path` (http request path), `method` (GET, POST, ..), and `id` (unique event ID).\n",
    "\n",
    "In some cases the events represent a record with a unique `key`, which can be read/set \n",
    "through the `event.key`.\n",
    "\n",
    "The task steps are called with the `event.body` by default. If a task step needs to \n",
    "read or set other event elements (key, path, time, ..) you should set the task `full_event`\n",
    "argument to `True`.\n",
    "\n",
    "Task steps support optional `input_path` and `result_path` attributes that allow controlling which portion of \n",
    "the event is sent as input to the step, and where to update the returned result.\n",
    "\n",
    "For example, for an event body `{\"req\": {\"body\": \"x\"}}`, `input_path=\"req.body\"` and `result_path=\"resp\"` \n",
    "the step gets `\"x\"` as the input. The output after the step is `{\"req\": {\"body\": \"x\"}: \"resp\": <step output>}`.\n",
    "Note that `input_path` and `result_path` do not work together with `full_event=True`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The context object\n",
    "\n",
    "The step classes are initialized with a `context` object (when they have `context` in their `__init__` args).\n",
    "The context is used to pass data and for interfacing with system services. The context object has the \n",
    "following attributes and methods.\n",
    "\n",
    "Attributes:\n",
    "* **logger**: Central logger (Nuclio logger when running in Nuclio).\n",
    "* **verbose**: True if in verbose/debug mode.\n",
    "* **root**: The graph object.\n",
    "* **current_function**: When running in a distributed graph, the current child function name.\n",
    "\n",
    "Methods:\n",
    "* **get_param(key, default=None)**: Get the graph parameter by key. Parameters are set at the\n",
    "  serving function (e.g. `function.spec.parameters = {\"param1\": \"x\"}`).\n",
    "* **get_secret(key)**: Get the value of a project/user secret.\n",
    "* **get_store_resource(uri, use_cache=True)**: Get the mlrun store object (data item, artifact, model, feature set, feature vector).\n",
    "* **get_remote_endpoint(name, external=False)**: Return the remote nuclio/serving function http(s) endpoint given its [project/]function-name[:tag].\n",
    "* **Response(headers=None, body=None, content_type=None, status_code=200)**: Create a nuclio response object, for returning detailed http responses.\n",
    "\n",
    "Example, using the context:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if self.context.verbose:\n",
    "    self.context.logger.info(\"my message\", some_arg=\"text\")\n",
    "    x = self.context.get_param(\"x\", 0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "vscode": {
   "interpreter": {
    "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
